package com.yunkeji.admin.controller.sys;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.yunkeji.common.annotation.Log;
import com.yunkeji.common.core.controller.BaseController;
import com.yunkeji.common.core.domain.PageQuery;
import com.yunkeji.common.core.domain.R;
import com.yunkeji.common.core.page.TableDataInfo;
import com.yunkeji.common.enums.BusinessType;
import com.yunkeji.dao.vo.sys.SysBannerVo;
import com.yunkeji.system.domain.bo.BannerBo;
import com.yunkeji.system.service.IBannerService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;

/**
 * banner
 *
 * @author 雷擎宇
 * @date 2024-12-09
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/banner")
public class BannerController extends BaseController {

    private final IBannerService bannerService;

    /** 查询列表 */
    @SaCheckPermission("system:sysBanner:list")
    @GetMapping("/list")
    public TableDataInfo<SysBannerVo> list(BannerBo bo, PageQuery pageQuery) {
        return bannerService.queryPageList(bo, pageQuery);
    }

    /** 新增 */
    @SaCheckPermission("system:sysBanner:add")
    @Log(title = "新增banner信息", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Void> add(@RequestBody BannerBo bo) {
        bannerService.insertByBo(bo);
        return R.ok();
    }

    /** 修改 */
    @SaCheckPermission("system:sysBanner:edit")
    @Log(title = "修改banner信息", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit(@RequestBody BannerBo bo) {
        bannerService.updateByBo(bo);
        return R.ok();
    }

    /**
     * 删除
     */
    @SaCheckPermission("system:sysBanner:remove")
    @Log(title = "删除banner信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public R<Void> remove(@PathVariable Long id) {
        bannerService.deleteWithValidByIds(Collections.singletonList(id), true);
        return R.ok();
    }

    /**
     * 切换启用禁用
     *
     * @param id 主键
     */
    @SaCheckPermission("system:sysBanner:enable")
    @Log(title = "切换banner启用禁用状态", businessType = BusinessType.UPDATE)
    @PutMapping("/{id}")
    public R<Void> enable(@PathVariable Long id) {
        bannerService.disable(id);
        return R.ok();
    }
}
